/**
 * This file is part of iSecurePasswords.
 *
 *   iSecurePasswords is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   iSecurePasswords is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with iSecurePasswords.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Matthew MacGregor
 * Created: 2012
 * iSecurePasswords, copyright 2013 Sudolink, LLC
 * 
 */ 
package com.sudolink.isp.app;

import java.io.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dev
 */
public class PropertiesManager {
    
    private static Properties properties;
    private static String appDataDirectory;
    
    public static final String SELECTED_DB_PATH = "selected.db.path";
    public static final String LAST_DB_PATH = "last.db.path";
    public static final String DEFAULT_PROPERTIES_FILENAME = "AppProperties";
    public static final String NUMBER_BACKUP_FILES_RETAINED = "backup.file.retention";
    public static final String UNLOCK_CODE_MD5 = "unlock.code.md5";
    public static final String MILLISECONDS_UNTIL_LOCKED = "ms.until.locked";
    public static final String EULA_SIGNED = "eula.accepted";
    public static final String DATA_INTEGRITY_SIGNATURE = "data.integrity.signature";
    
    //Look for the properties file in the expected location
    //If not found, create a default
    //If found, return
    
    private static Properties generateDefaultProperties() throws IOException {
        if ( properties == null ) {
            properties = new Properties();
        }
//        properties.setProperty(SELECTED_DB_PATH, "" );
        properties.setProperty(LAST_DB_PATH, "");
        properties.setProperty(NUMBER_BACKUP_FILES_RETAINED, "7");
        
        return properties;
        
    }
    
    
    public static Properties getInstance() throws IOException  {
        if ( properties == null ) {
//            initialize();
            try {
                load();
            } catch (IOException e) {
                Logger.getLogger(PropertiesManager.class.getName()).log( Level.INFO, "The properties file was not available.");
                Logger.getLogger(PropertiesManager.class.getName()).log(Level.INFO, null, e);
                properties = generateDefaultProperties();
            }
        }
        return properties;
    }
    
    private static void initialize() {
        if ( appDataDirectory == null ) {
            appDataDirectory = Application.getPathToAppDataDirectory();
        }
    }
    
    public static void commit() throws IOException {
        initialize();
        File appDataDirFile = new File(appDataDirectory, DEFAULT_PROPERTIES_FILENAME);
        FileWriter fw = new FileWriter(appDataDirFile);
        
        if ( appDataDirFile.exists() == false  ) {
            appDataDirFile.mkdirs();
        }
        if ( properties != null ) {
            properties.store(fw, "Properties for this application.");
        }
        
        
    }
    
    private static void load() throws IOException {
        
        initialize();
        
        File pathToAppDataDirectory = new File(appDataDirectory);
        
        if ( pathToAppDataDirectory.exists() == false )
        {
            pathToAppDataDirectory.mkdirs();
        }
        
        FileInputStream in = null;
        try {

            // create application properties with default
            Properties applicationProps = new Properties();
            File appPropsFile = new File(pathToAppDataDirectory,DEFAULT_PROPERTIES_FILENAME);
            if ( appPropsFile.exists() ) {
                in = new FileInputStream(appPropsFile);
                applicationProps.load(in);
                in.close();
            } else {
                applicationProps = generateDefaultProperties();
            }
        
            properties = applicationProps;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PropertiesManager.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if ( in != null ) {
                    in.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(PropertiesManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
            
    
    
    
}
